package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.FoodDao;
import com.itheima.dao.SportsDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sports;
import com.itheima.service.SportsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author HLJ
 */
@Service(interfaceClass = SportsService.class )
@Transactional
public class SportsServiceImpl implements SportsService{
    @Autowired
    private SportsDao sportsDao;

    @Override
    public void upload(List<Sports> sports) {
        if (sports != null && sports.size()>0){
            for (Sports sport : sports) {
                int countById = sportsDao.findCountById(sport.getId());
                if (countById>0){
                    sportsDao.edit(sport);
                }
                sportsDao.add(sport);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);

        Page<Sports> page = sportsDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Sports findById(Integer id) {
       return sportsDao.findById(id);
    }

    @Override
    public void add(Sports sports) {
        sportsDao.add(sports);
    }

    @Override
    public void edit(Sports sports) {
        sportsDao.edit(sports);
    }

    @Override
    public void delete(Integer id) {
        sportsDao.delete(id);
    }
}
